package com.feiyuan.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feiyuan.annotation.FeiyuanRequiresPermissions;
import com.feiyuan.common.utils.JWTUtils;
import com.feiyuan.modules.sys.vo.GeneratorRoutersVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import com.feiyuan.modules.sys.entity.MenuEntity;
import com.feiyuan.modules.sys.service.MenuService;
import com.feiyuan.common.utils.PageUtils;
import com.feiyuan.common.utils.ResponseUtils;

import javax.servlet.http.HttpServletRequest;


/**
 * 菜单管理
 *
 * @author Xupf
 * @email xupengfei131276@126.com
 * @date 2024-03-01 14:51:34
 */
@RestController
@RequestMapping("/admin/sys/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    /**
     * 列表
     */
//    @RequestMapping("/list")
//    @RequiresPermissions("sys:menu:list")
    public ResponseUtils list1(@RequestParam Map<String, Object> params){
        PageUtils page = menuService.queryPage(params);

        return ResponseUtils.ok().put("page", page);
    }

    /**
     * 查询菜单列表
     * @param params
     * @return
     */
    @GetMapping("/list")
    @FeiyuanRequiresPermissions("sys:menu:list")
    public ResponseUtils list(@RequestParam Map<String, Object> params) {
        return ResponseUtils.ok().put("menus", menuService.listMenus(params));
    }

    /**
     * 查询相信信息中的菜单列表
     * @return
     */
    @GetMapping("/selectParentId")
    public ResponseUtils selectParentId() {
        return ResponseUtils.ok().put("menu", menuService.selectParentId());
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{menuId}")
//    @RequiresPermissions("sys:menu:info")
    public ResponseUtils info(@PathVariable("menuId") Long menuId){
		MenuEntity menu = menuService.getById(menuId);

        return ResponseUtils.ok().put("menu", menu);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:menu:save")
    public ResponseUtils save(@RequestBody MenuEntity menu){
		menuService.save(menu);

        return ResponseUtils.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:menu:update")
    public ResponseUtils update(@RequestBody MenuEntity menu){
        menu.setUpdateTime(new Date( ));
		menuService.updateById(menu);

        return ResponseUtils.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:menu:delete")
    public ResponseUtils delete(@RequestBody Long[] menuIds){
        // 校验是否能删除
        for (Long menuId : menuIds) {
            List<MenuEntity> parentId = menuService.list(new QueryWrapper<MenuEntity>().eq("parent_id", menuId));
            if (!CollectionUtils.isEmpty(parentId)) {
                return ResponseUtils.error(500, "所选菜单中存在下级菜单，无法删除，请先删除下级菜单!");
            }
        }
		menuService.removeByIds(Arrays.asList(menuIds));

        return ResponseUtils.ok();
    }

    /**
     * 获取首页的左侧菜单
     * @return
     */
    @GetMapping("/nav")
    public ResponseUtils nav(HttpServletRequest request) {
        // 获取当前用户的ID
        String userId = JWTUtils.getMemberIdByJwtToken(request);
        List<GeneratorRoutersVO> lists = menuService.nav(userId);
        return new ResponseUtils().put("result", lists);
    }

}
